import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/game_item.dart';
import 'package:flutter_syt/domain/games/SportList.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/widgets/view_state_widget.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

@RoutePage(name: "sportsDetail")
class SportsDetailPage extends StatefulWidget {
  final String id;
  final String name;
  SportsDetailPage(this.id, this.name);
  @override
  State<StatefulWidget> createState() {
    return _SportsDetailState();
  }
}

class _SportsDetailState extends State<SportsDetailPage> {
  GameDetail? data;
  bool isLoading = true;
  String? error;
  @override
  void initState() {
    getData();
    super.initState();
  }

  getData() async {
    setState(() {
      isLoading = true;
    });
    try {
      data = await API.shared.game.itemDetail(widget.id);
      error = null;
    } catch (e) {
      error = "$e";
    }
    setState(() {
      isLoading = false;
    });
  }

  Widget loading() {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            "assets/icons/empty.png",
            width: 80,
            height: 80,
          ),
          Container(
            height: 12,
          ),
          Text(
            S.of(context).loading,
            style:
                TextStyle(color: Colors.black.withOpacity(0.4), fontSize: 14),
          ),
          Container(
            height: 20,
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    Widget content;
    if (isLoading) {
      content = Center(
        child: loading(),
      );
    } else if (error != null) {
      content = ViewStateErrorWidget(
        onPressed: ClickUtils.debounce(() {
          getData();
        }),
      );
    } else {
      content = SingleChildScrollView(
        child: Html(data: data?.detail ?? ""),
      );
    }
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.name),
      ),
      body: content,
    );
  }
}
